package ge.modules.events;

import java.util.HashMap;

import ge.engine.Log;
import ge.exceptions.FunctionNonSuported;
import ge.lowlevel.EventEngine;
import ge.lowlevel.IOEngine;

public class IOEventModule extends EventModule 
{
	private HashMap<String, Boolean> mouseUpEvent = new HashMap<String, Boolean>();
	private HashMap<String, Boolean> keyDown = new HashMap<String, Boolean>();
	private HashMap<String, Boolean> keyUp = new HashMap<String, Boolean>();
	
	//private static IOEventModule instance;
	public IOEventModule()
	{
		super();
		attachMethod("keyPressed");
		attachMethod("keyDown");
		attachMethod("keyUp");
		attachMethod("mouseDown");
		attachMethod("mouseUp");
		attachMethod("mouseX");
		attachMethod("mouseX");
	}
	
	/*public static IOEventModule get()
	{
		if(instance == null)
			instance = new IOEventModule();
		
		return instance;
	}*/
	@Override
	public void update() {
		mouseUpEvent.clear();
		keyDown.clear();
		keyUp.clear();
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub

	}
	
	/**
	 * 	Checks if the key is pressed
	 * 	It doesn't differences capital letter
	 * @param key Key to ask.
	 * @return True if its checked
	 */
	public boolean keyPressed(String key)
	{
	 
			try {
				return IOEngine.get().keyPressed(key);
			} catch (FunctionNonSuported e) {
				e.printStackTrace();
				return false;
			}
		 
	}
	/**
	 * 	Checks if the key is pressed just in this moment
	 * 	It doesn't differences capital letter
	 * @param key Key to ask.
	 * @return True if its checked
	 */
	public boolean keyDown(String key)
	{
		if(keyDown.get(key) != null)
		{
			return keyDown.get(key);
		}
		try {
		 
			keyDown.put(key,IOEngine.get().keyDown(key));
			return keyDown.get(key);
			
		} catch (FunctionNonSuported e) {
			return false;
		}
	}
	/**
	 * 	Checks if a pressed key it's being dropped
	 * 	It doesn't differences capital letter
	 * @param key Key to ask.
	 * @return True if its checked
	 */
	public boolean keyUp(String key)
	{
		if(keyUp.get(key) != null)
		{
			return keyUp.get(key);
		}
		try {
		 
			keyUp.put(key,IOEngine.get().keyUp(key));
			return keyUp.get(key);
			
		} catch (FunctionNonSuported e) {
			return false;
		}
	}
	/**
	 *  Checks if the mouse button is pressed
	 * 	"LEFT" or "RIGHT" or "MIDDLE"
	 * @param button button to ask.
	 * @return True if its checked
	 */
	public boolean mouseDown(String button)
	{
		
		try {
			return IOEngine.get().mouseDown(button);
		} catch (FunctionNonSuported e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 *  Checks if a pressed button it's being dropped
	 * 	"LEFT" or "RIGHT"
	 * @param button button to ask.
	 * @return True if its checked
	 */
	public boolean mouseUp(String button)
	{
		if(mouseUpEvent.get(button) != null)
		{
			return mouseUpEvent.get(button);
		}
		
		try {
			mouseUpEvent.put(button,IOEngine.get().mouseUp(button));
			return mouseUpEvent.get(button);
			
		} catch (FunctionNonSuported e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 	The position x of the mouse
	 * @return The position x
	 * @throws FunctionNonSuported if the engine doesn't support it
	 */
	public double mouseX()
	{
		try {
			return IOEngine.get().mouseX();
		} catch (FunctionNonSuported e) {
			 
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 	The position y of the mouse
	 * @return The position y
	 * @throws FunctionNonSuported if the engine doesn't support it
	 */
	public double mouseY()
	{
		try {
			 
			return IOEngine.get().mouseY();
		} catch (FunctionNonSuported e) {
		 
			e.printStackTrace();
		}
		return 0;
	}
	
	@Override
	public void delete()
	{
		
	}
}
